﻿using UnityEngine;
using System.Collections;
using Engine.Entities.Character;
using FrenzyIsland.UI;
using Game.Scripts.Managers;

[RequireComponent (typeof(SelectedCharacterModel))]
public class SelectedCharacterController: MonoBehaviour {

    public UIAtlas atlasIcon;
    public UIGrid gridInfo;

    private bool requireRefresh = false;

	void Start () {
        requireRefresh = true;
    }
	
	void Update () {

        if( requireRefresh )
        {
            SteveBrain brain = GetComponent<SelectedCharacterModel>().steveBrain;

            //creo la sprite per il gender
            string genderName = brain.model.Gender == GenderEnum.Male ? "gender-male" : "gender-female";
            createSpriteInGrid(atlasIcon, genderName, "gender");

            //creo la sprite per il lavoro
            if(brain.workplace != null)
            {
                UIAtlas atlasName = brain.workplace.prototype.atlasName;
                string iconName = brain.workplace.prototype.iconName;
                createSpriteInGrid(atlasName, iconName, "work");
            }

            //creo la sprite per la comitiva
            if( brain.party != null )
            {
                createSpriteInGrid(atlasIcon, "party-icon", "party");
            }
            

            gridInfo.Reposition();

            requireRefresh = false;
        }

	}

    private void createSpriteInGrid(UIAtlas atlas, string name, string label)
    {
        GameObject spriteGender = new GameObject(label);
        UISprite sprite = spriteGender.AddComponent<UISprite>();
        sprite.atlas = atlas;
        sprite.spriteName = name;
        sprite.depth = 2;
        spriteGender.transform.parent = gridInfo.transform;
        sprite.GetComponent<UIWidget>().width = 50;
        sprite.GetComponent<UIWidget>().height = 50;
        sprite.gameObject.transform.localScale = Vector3.one;
    }

    public void btnQuit_Click()
    {
        GuiManager gui = UIRoot.list[0].GetComponent<GuiManager>();
        gui.CloseWidget(this.gameObject);
    }

    

}
